package cate.game.demo.merge;

import cate.game.GameBody;
import cate.game.common.merge.MergeTool;
import cate.game.db.facade.KvLoader;
import cate.game.db.mongo.kv.KvMongo;
import cate.game.demo.spo.DemoStatus;
import cp.solution.business.game.conf.MergeLogEntity;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DemoMergeLocal {

    public void initIndependent(GameBody game) throws Exception {
        // 自定义 合服 操作
        MergeTool.handle(game, mls -> {
            // 执行自己模块需要的操作
            for(MergeLogEntity ml : mls) {
                log.info("demo模块 处理 合并过来的服 srvId={} ( {} ) 老的跨服({}) 变成 新的跨服({})",
                        ml.getSrvId(), ml.getSrvName(), ml.getCrossCodeOld(), ml.getCrossCodeNew());
                // 跨服排行清楚老数据
                if (!ml.getCrossCodeOld().equals(ml.getCrossCodeNew())) {
//                    game.crossRank.getMessageModule().noticeMergeChangeCross(ml.getCrossCodeOld(), ml.getSrvId());
                }
            }
        });
    }

    public DemoStatus loadStatus(GameBody game) throws Exception {
        DemoStatus status = null;
        int mode = 3;
        switch (mode) {
            case 0:
                status = new KvLoader<>(game, DemoStatus.class, KvMongo.KEY_DEMO).getFirst();
                break;
            case 1:
                status = new KvLoader<>(game, DemoStatus.class, KvMongo.KEY_DEMO).getBySrvId();
                break;
            case 2:
                status = new KvLoader<>(game, DemoStatus.class, KvMongo.KEY_DEMO).getBySrvId(false);
                break;
            case 3:
                status = new KvLoader<>(game, DemoStatus.class, KvMongo.KEY_DEMO).getWithMerge((olds) -> {
                    DemoStatus _status = new DemoStatus();
                    for(DemoStatus old : olds) {
                        _status.roleIds.addAll(old.roleIds);
                    }
                    return _status;
                });
                break;
            default:
                break;
        }
        log.info("最终这个status={}", status);
        return status;
    }

}
